/* 
 *  <copyright file="PositionCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using SobrietyEngine.Common;

namespace SobrietyEngine.Components.Common
{
    public class PositionCmp:BaseComponent, IPosition, IComponent
    {
        protected Vector3 positionV3;
        public Vector3 PositionV3 { get { return positionV3; } set { positionV3 = value; } }
        public Vector2 PositionV2 { get { return Get(); } set { Set(value); } }

        public PositionCmp()
            : base()
        {
        }

        public PositionCmp(Vector2 pos)
            : base()
        {
            positionV3.X = pos.X;
            positionV3.Y = pos.Y;
        }

        public PositionCmp(Vector3 pos)
            : base()
        {
            positionV3 = pos;
        }

        public override List<ParameterDescription> GetParameterDescriptions()
        {
            List<ParameterDescription> d = new List<ParameterDescription>();
            d.Add(new ParameterDescription("X", "X position value", typeof(float), (float)0));
            d.Add(new ParameterDescription("Y", "Y position value", typeof(float), (float)0));
            d.Add(new ParameterDescription("Z", "Z position value", typeof(float), (float)0));
            d.Add(new ParameterDescription("XYZ", "X,Y,Z position value", typeof(float), (float)0));
            return d;
        }

        override public void Set(Parameters parms)
        {
            positionV3.X = parms.GetValueAs<float>("X", 0f);
            positionV3.Y = parms.GetValueAs<float>("Y", 0f);
            positionV3.Z = parms.GetValueAs<float>("Z", 0f);

            //handle the vector3 parameter
            positionV3 = parms.GetValueAs<Vector3>("XYZ", new Vector3(0, 0, 0));
        }

        public override bool InitComponent()
        {
            //we dont need to update.
            Active = false;
            return true;
        }

        override public Type[] SupportedInterfaces()
        {
            return new Type[] { typeof(IPosition) };
        }
        
        public void Set(Vector3 pos)
        {
            positionV3 = pos;
        }

        public void Add(Vector3 pos)
        {
            positionV3 += pos;
        }

        public Vector3 GetV3()
        {
            return positionV3;
        }

        public void Set(Vector2 pos)
        {
            positionV3.X = pos.X;
            positionV3.Y = pos.Y;
            positionV3.Z = 0;
        }

        public void Add(Vector2 pos)
        {
            positionV3.X += pos.X;
            positionV3.Y += pos.Y;
            //Position.Z = 0;
        }

        public Vector2 Get()
        {
            return new Vector2(positionV3.X, positionV3.Y);
        }
    }
}